﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TGTTVK04
    {
        public TGTTVK04()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKKOPF();
            this.DataContext = MV;
            InitializeComponent();
            SACHBE = app.User;
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            IsAUFConfirmed = false;
            dwData = TBMain.NewRow();
            dwData["VKKMANDANT"] = app.Mandant;
            dwData["VKKAUFKRS"] = KRS;
            dwData["VKKAUFLFD"] = LFD;
            if (this.ScopeManage.Trim() == "*" || ScopeManage.Trim() == "")
            {
                dwData["VKKSACHBE"] = "";
            }
            else
            {
                string[] split = ScopeManage.Split(';');
                string m = "";
                foreach (string s in split)
                {
                    if (s.Trim().Length == 0) continue;
                    m += "VKKSACHBE='" + s.Trim().PadRight(8) + "' OR ";
                }
                m += "***";
                m = m.Replace("OR ***", "");
                m = " AND (" + m + ")";
                dwData["VKKSACHBE"] = m;
            }

            SResult = TMV.QVKK01(dwData);
            MergeData(SResult, true);
        }

       
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {

                if (!IsNew)
                {
                    IsAUFConfirmed = false;
                    cSD8884.Reset();
                    cSD0149.Reset();
                    cSHR20.Reset(); 
                    cKD02.Reset();
                }
                base.NewCommand(sender, e);
                vwMaster.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

            DataSet Set = new DataSet();
            dwData = TMV.TBMain.Rows[0]; 
            string TYPE = dwData["VKKAUFART"].ToString().Trim();
            if (TYPE == "N" ||  TYPE=="NA")
            {
                cDR.DocType = "TSTVK05";
                Set.Merge(TMV.QueryVKKOPF(dwData, 5));  
            }
            else
            {
                cDR.DocType = "TSTVKSMP";
                Set.Merge(TMV.QueryVKKOPF(dwData, 6));
                dwData = VVKPOS.Instance.TBMain.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                dwData["VKPAUFKRS"] = TMV.TBMain.Rows[0]["VKKAUFKRS"].ToString().Trim();
                dwData["VKPAUFLFD"] = TMV.TBMain.Rows[0]["VKKAUFLFD"].ToString().Trim();
                Set.Merge(VVKPOS.Instance.QVKP26(dwData));
            }

            string TAX = Set.Tables["VKKOPF"].Rows[0]["ACCOUNT"].ToString().Trim();
            dwData = VLIEFKO.Instance.TBMain.NewRow();
            dwData["LSKMANDANT"] = app.Mandant;
            dwData["LSKLIEFNR"] = " AND LSKLIEFNR='00000000'";
            dwData["LSKKONDSL"] = " AND TRIM(LSKKONDSL)='" + TAX + "'";
            Set.Merge(VLIEFKO.Instance.QueryLIEFKO(dwData, 12));          
            cDR.PRINT(Set);
        } 
    }
}
